﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Telerik.Web.Mvc;
using XStudio.XCRF.Data.Business;
using XStudio.XCRF.Data.Business.Permissions;
using XStudio.XCRF.Data.Entities;
using XStudio.XCRF.Web.Common.Sessions;
using XStudio.XCRF.Web.Models.Hospital;
namespace XStudio.XCRF.Web.Controllers
{
    /// <summary>
    /// 主控制器
    /// </summary>
    public class HomeController : BaseController
    {
        [DefaultPermission(PermissionType.Allow)]
        public ActionResult Index()
        {
            if (IsMemberFirst)  //用户第一次登陆
                return RedirectToAction<MemberController>(c => c.MemberEditSelf());
            if (IsHospitalFirst && !LoginSession.IsResearcher)    //医院第一次登陆
                return RedirectToAction<HospitalController>(c => c.HospitalEdit((Guid)HospitalID));
            return View();
        }
        [DefaultPermission(PermissionType.Allow)]
        [GridAction]
        //[GridAction(EnableCustomBinding=true)]
        public ActionResult _Index()
        {
            List<Hospital> hospitals = DataContext.Hospitals
                //.Include(h => h.Members)
                .GetMasterRecords()
                .OrderBy(hospital => hospital.Code)
                .ToList();
            List<HospitalListModel> dtos = (from hospital in hospitals
                                            select new HospitalListModel
                                            {
                                                ID = hospital.ID,
                                                //Code = hospital.Code,
                                                CRFCount = hospital.CRFs.GetMasterRecords().Count(),
                                                Name = hospital.Name,
                                                /*Owner = new Member
                                                {
                                                    ID = hospital.Owner.ID,
                                                    RealName = hospital.Owner.RealName
                                                }*/
                                            }).ToList();
            return Json(new GridModel<HospitalListModel>
            {
                Data = dtos,
                Total = dtos.Count()
            });
        }
    }
}

